package com.bcbim.api_web.sys.redis;

import com.alibaba.fastjson.parser.ParserConfig;
import com.alibaba.fastjson.support.spring.FastJsonRedisSerializer;
import lombok.extern.slf4j.Slf4j;
import org.omg.CORBA.Object;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * @Description: redis配置类
 * @Author haoshuai
 * @Date 2020/9/16
 * @Version V1.0
 **/
@Slf4j
@Configuration
public class RedisConfig {

    @Bean(name="redisTemplate")
    public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {

        log.info("Starting injection redisTemplate");

        RedisTemplate<Object, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(redisConnectionFactory);

        ParserConfig.getGlobalInstance().setAutoTypeSupport(true);

        FastJsonRedisSerializer fastJsonRedisSerializer = new FastJsonRedisSerializer(java.lang.Object.class);

        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();

        // value 序列化采用 FastJsonRedisSerializer
        template.setValueSerializer(fastJsonRedisSerializer);
        template.setHashValueSerializer(fastJsonRedisSerializer);

        // key 序列化采用 StringRedisSerializer
        template.setKeySerializer(stringRedisSerializer);
        template.setHashKeySerializer(stringRedisSerializer);

        template.afterPropertiesSet();

        return template;
    }
}
